
/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

package com.jy.rhin.facade.ws.document.storage;

import cn.hutool.core.collection.CollectionUtil;
import cn.org.chiss.rhin._2015.base.OID;
import cn.org.chiss.rhin._2015.base.UUID;
import cn.org.chiss.rhin._2015.base.document.DocumentReference;
import cn.org.chiss.rhin._2015.base.document.StoredDocument;
import cn.org.chiss.rhin._2015.base.document.SubmissionSet;
import cn.org.chiss.rhin._2015.document.management.RegisterDocumentSet;
import cn.org.chiss.rhin._2015.document.repository.ProvideAndRegisterDocumentSet;
import cn.org.chiss.rhin._2015.document.repository.ProvideAndRegisterDocumentSetResponse;
import cn.org.chiss.rhin._2015.document.repository.RetrieveDocumentSet;
import cn.org.chiss.rhin._2015.document.repository.RetrieveDocumentSetResponse;
import com.jy.framework.base.utils.Serializer;
import com.jy.rhin.application.assembler.DocumentManagementAssembler;
import com.jy.rhin.application.document.StorageService;
import com.jy.rhin.config.NamespaceConstants;
import com.jy.rhin.config.RhinFaultSubCode;
import com.jy.rhin.config.ws.WsClientFactory;
import com.jy.rhin.facade.ws.basic.wsEntry.exception.RhinWSException;
import com.jy.rhin.facade.ws.document.management.DocumentManagement;
import com.jy.rhin.infra.repo.document.content.query.DocumentContentQueryHandler;
import com.jy.rhin.sdk.dto.document.DocumentProvideAndRegisterDto;
import com.jy.rhin.sdk.dto.document.query.GetContentDto;
import com.jy.rhin.sdk.dto.document.query.GetContentReqDto;
import com.jy.rhin.sdk.dto.document.query.GetContentRespDto;
import com.jy.rhin.support.aspect.AuditLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.jws.WebService;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;


/**
 * This class was generated by Apache CXF 3.5.1
 * 2022-04-18T15:21:44.686+08:00
 * Generated source version: 3.5.1
 *
 * @author dhl
 */
@Slf4j
@WebService(
        serviceName = "DocumentStorageService",
        portName = "DocumentStoragePort",
        targetNamespace = NamespaceConstants.NS_RHIN,
        endpointInterface = "com.jy.rhin.facade.ws.document.storage.DocumentStorage")
@Component
public class DocumentStoragePortImpl implements DocumentStorage {

    @Resource
    WsClientFactory wsClientFactory;
    @Resource
    StorageService storageService;
    @Resource
    private DocumentContentQueryHandler documentContentQueryHandler;

    /**
     * 文档提交并注册
     *
     * @param message
     * @return
     */
    @Override
    @AuditLog
    public ProvideAndRegisterDocumentSetResponse providerAndRegistryDocumentSet(ProvideAndRegisterDocumentSet message) {
        log.info("Executing operation providerAndRegistryDocumentSet:{}", Serializer.serialize(message));
        //文档提交
        ProvideAndRegisterDocumentSetResponse resp = new ProvideAndRegisterDocumentSetResponse();
        try {
            DocumentProvideAndRegisterDto provideDto = storageService.documentProvide(message);
            SubmissionSet submissionSet = DocumentManagementAssembler.dtoConvert(message.getSubmissionSet(), provideDto.getSubmissionSet());
            //文档注册
            UUID result = registerDocumentSet(submissionSet);
            resp.setSubmissionSetUUID(result);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new RhinWSException("ProvideAndRegisterDocumentSetFault", ex.getMessage());
        }

        return resp;
    }

    /**
     * 文档索引获取
     *
     * @param document
     * @return
     */
    @Override
    @AuditLog
    public RetrieveDocumentSetResponse retrieveDocumentSet(RetrieveDocumentSet document) {
        log.info("Executing operation retrieveDocumentSet:{}", Serializer.serialize(document));
        List<GetContentDto> list = document.getDocumentReference().stream().map(this::paramConvert).collect(Collectors.toList());
        GetContentReqDto dto = new GetContentReqDto();
        dto.setDtoList(list);
        List<GetContentRespDto> contentList = documentContentQueryHandler.getDocumentContentList(dto);
        if (CollectionUtil.isEmpty(contentList)) {
            throw new RhinWSException(RhinFaultSubCode.RetrieveDocumentSetFault);
        }
        RetrieveDocumentSetResponse response = new RetrieveDocumentSetResponse();
        if (!CollectionUtils.isEmpty(contentList)) {
            response.setDocument(contentList.stream().map(this::resultConvert).collect(Collectors.toList()));
        }
        return response;
    }

    /**
     * 调用文档管理服务的文档注册接口
     *
     * @param submissionSet
     * @return
     */
    private UUID registerDocumentSet(SubmissionSet submissionSet) {
        RegisterDocumentSet registerDocumentSet = new RegisterDocumentSet();
        registerDocumentSet.setSubmissionSet(submissionSet);
        return wsClientFactory.getClient(DocumentManagement.class).registerDocumentSet(registerDocumentSet).getSubmissionSetUUID();
    }

    private StoredDocument resultConvert(GetContentRespDto dto) {
        StoredDocument storedDocument = new StoredDocument();
        storedDocument.setDocumentUniqueId(new OID().setValue(dto.getDocumentUniqueId()));
        storedDocument.setRepositoryUniqueId(new OID().setValue(dto.getRepositoryUniqueId()));
        storedDocument.setContent(Base64.getDecoder().decode(dto.getContent()));
//        storedDocument.setContent("content内容");
        return storedDocument;
    }

    private GetContentDto paramConvert(DocumentReference documentReference) {
        GetContentDto dto = new GetContentDto();
        dto.setUniqueId(documentReference.getDocumentUniqueId().getValue());
        dto.setRepositoryUniqueId(documentReference.getRepositoryUniqueId().getValue());
        return dto;
    }
}
